/**
 * 
 */
package org.waddy.framework.pub;

import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title RuleEngine.java
 * @version 1.0
 * @author DapengZhang
 * @Created 2012-12-27
 * Copyright (c) 2012 PanGene InfoAnalytics.
 *
 * @Description: 规则引擎，目前用于文本信息分组
 */
public class RuleEngine {
	/**
	 * 主测试方法
	 */
	public static void main(String[] args) {
		testTextMarch();
//		testBuildRule();
	}
	
	public static void testTextMarch(){
		//规则文本
		String ruleText = "(信息)(你好,中国)(韩国,朝鲜,俄罗斯)|(网络)|(新闻,广播,广告)(煤炭,钢铁)";
		//输入文本
		String inputText1 = "...网络...";
		String inputText2 = "...你好...韩国...";
		String inputText3 = "...你好...朝鲜...";
		String inputText4 = "...你好...朝鲜...网络...";
		String inputText5 = "...你好...朝鲜...信息...";
		String inputText6 = "...你好...朝鲜...信息...网络...";
		String inputText7 = "...你好...朝鲜...新闻...";
		String inputText8 = "...你好...朝鲜...新闻...钢铁...";
		String[] inputText = new String[]{inputText1,inputText2,inputText3,inputText4,inputText5,inputText6,inputText7,inputText8};
		//解析规则文本
		Vector expressions = buildRule(ruleText);
		for(int i=0; i<inputText.length; i++){
			if(_march(inputText[i],expressions)){
				System.out.println("匹配成功："+inputText[i]);
			}else{
				System.out.println("匹配失败："+inputText[i]);
			}
		}
		
	}
	
	public static void testBuildRule(){
		String ruleText = "(中国123)(你好,12_A阿)(a你好a,bc中,2国3)|(煤炭,钢铁)(李四,王二)";
		buildRule(ruleText);
	}
	
	public static boolean march(String inputText, String ruleText){
		boolean marchFlag = false;
		//解析规则
		Vector expressions = buildRule(ruleText);
		return _march(inputText,expressions);
	}
	
	public static boolean _march(String inputText, Vector expressions){
		boolean marchFlag = false;
		for(int i=0; i<expressions.size(); i++){
			Vector expression = (Vector) expressions.get(i);
			boolean expressionMarchFlag = true;
			//System.out.println("---------------第"+i+"个表达式，共"+expression.size()+"个词组");
			for(int j=0; j<expression.size(); j++){
				String[] group =(String[]) expression.get(j);
				boolean grroupMarchFlag = false;
				//System.out.println("第"+j+"个词组："+Arrays.toString(group));
				for(int k=0; k<group.length; k++){ //一个词组中只要有一个词满足条件（被包含），则该词组及匹配
					if(inputText.contains(group[k])){
						grroupMarchFlag = true;
						break;
					}
				}
				expressionMarchFlag = expressionMarchFlag && grroupMarchFlag;
			}
			marchFlag = marchFlag || expressionMarchFlag;
		}
		
		return marchFlag;
	}
	
	public static Vector buildRule(String ruleText){
//		//分组规则：括号内-或者关系；括号间-并且关系
//		String groupRuleText = "(中国123)(你好,12_A阿)(a你好a,bc中,2国3)";
//		String groupRuleText1 = "(中国123)";
//		String groupRuleText2 = "(中国123)(你好,12_A阿)";
//		String groupRuleText3 = "(中国123)(你好,12_A阿)(a你好a,bc中,2国3)";
//		String groupRuleText4 = "(中国123)(你好,12_A阿)(a你好a,bc中,2国3)|(煤炭,钢铁)(李四,王二)";
//		
//		/**
//		 * 分组规则的构成：
//		 * 0.元字母：中竖线、圆括号、逗号
//		 * 1.一级构成：包含规则表达式、排除规则表达式
//		 * 2.二级构成：括号-词组、逗号-单词
//		 * 3.组合关系：包含表达式（括号内或者关系、括号间并且关系），排除表达式（括号内并且关系、括号间或者关系）
//		 * 分组步骤：
//		 * 1.解析分组规则
//		 * 2.对文本进行匹配
//		 * 2.1匹配每个规则单元
//		 * 2.2集成匹配结果（包含规则集成、排除规则集成）
//		 */
//		
		String signalWordPattern = "[\u4E00-\u9FA5\\w]+";
//		System.out.println("wordPattern:"+Pattern.matches(signalWordPattern, "你好12中国a"));
		String wordGroupPattern = "[\u4E00-\u9FA5\\w,"+"]+";
//		System.out.println("wordGroupPattern:"+Pattern.matches(wordGroupPattern, "a你好a,bc中,2国3"));
		String wordGroupsPattern = "(\\(("+wordGroupPattern+")+\\))+";
//		System.out.println("wordGroupsPattern:"+Pattern.matches(wordGroupsPattern, "(中国123)(你好,12_A阿)(a你好a,bc中,2国3)"));
		String allPattern = "["+wordGroupsPattern+"|]+";
		
		boolean flag = Pattern.matches(allPattern,ruleText);
		System.out.println("allPattern:"+flag);
		System.out.println("ruleText:"+ruleText);
		Vector expressions = new Vector();
		if(flag){
//			System.out.println("rule: "+ruleText);
			String[] _expressions = ruleText.split("[|]");
//			System.out.println("_expressions="+Arrays.toString(_expressions));
			Pattern pattern = Pattern.compile(wordGroupPattern);
			for(int i=0; i<_expressions.length; i++){
				Vector expression = new Vector();
				Matcher matcher = pattern.matcher(_expressions[i]);
				while(matcher.find()){
					String _group = matcher.group();
					expression.add(_group.split(","));
				}
				expressions.add(expression);
			}
//			System.out.println("共"+expressions.size()+"个表达式");
//			for(int i=0; i<expressions.size(); i++){
//				Vector expression = (Vector) expressions.get(i);
//				System.out.println("---------------第"+i+"个表达式，共"+expression.size()+"个词组");
//				for(int j=0; j<expression.size(); j++){
//					String[] group =(String[]) expression.get(j);
//					System.out.println("第"+j+"个词组："+Arrays.toString(group));
//				}
//			}
		}
		return expressions;
	}
	
}
